package com.ehouse.adapters;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ehouse.R;
import com.ehouse.database.TemperatureScheduleTable;
import com.ehouse.models.Settings;

public class ScheduleTemperatureItemAdapter extends ResourceCursorAdapter{
	private int layout;
 
	public ScheduleTemperatureItemAdapter (Context context, int layout, Cursor c, int flags){
		super(context, layout, c, flags);
		this.layout = layout;
	}

	    @Override
	    public View newView(Context context, Cursor cur, ViewGroup parent) {
	        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        return li.inflate(layout, parent, false);
	    }

	    @Override
	    public void bindView(View view, Context context, Cursor cursor) {
	    	int temp = cursor.getInt(cursor.getColumnIndex(TemperatureScheduleTable.COLUMN_TEMP));
	    	
	    	final SharedPreferences sharedPrefs = PreferenceManager
					.getDefaultSharedPreferences(context);
	    	
	    	boolean fahrenheit = (sharedPrefs.getString(Settings.TEMP_UNIT, "0").equals("1"));
	    	String unit = context.getResources().getString(R.string.unit_c);
			if(fahrenheit){
				temp =  (((temp/5)*9)+32);
				unit = context.getResources().getString(R.string.unit_f);
			}
			
			String time = temp + " " + unit;
	    	String detail = context.getResources().getString(R.string.temp_detail);
	    	TextView scheduleLabel = (TextView)view.findViewById(R.id.scheduleLabel);
	    	TextView detailLabel = (TextView)view.findViewById(R.id.detailLabel);
	    	scheduleLabel.setText(time);
	    	detailLabel.setText(detail);
	    }
}